//
//  BaseRequestAPI.m
//  zhima_kt
//
//  Created by zhima on 2020/3/5.
//  Copyright © 2020 zhima. All rights reserved.
//
#import "NSData+AES.h"

#import "BaseRequestAPI.h"
#import "HeaderModel.h"
#import "AESCipher.h"
#import "ResponseModel.h"

#define kPwd @"1k56pol_=qweqp/e"

@implementation BaseRequestAPI

-(instancetype)init{
    self = [super init];
    if (self) {
        self.isOpenAES = YES;
    }
    return self;
}

#pragma mark ————— 自定义数据 —————

- (NSString *)message {
    if (self.error) {
        return self.error.localizedDescription;
    }
    NSString *message = [NSString stringWithFormat:@"%@",self.result.msg];
    return message;
}
- (NSString *)code {
    NSString *code = [NSString stringWithFormat:@"%ld",(long)self.result.code];
    return code;
}
- (BOOL)isSuccess {
    
    NSString *code = [self code];
    
    BOOL isSuccess = NO;
    
    if ([code isEqualToString:@"200"]) {
        isSuccess = YES;
    } else if ([code isEqualToString:@"102"]) {
        //账号被顶掉
//        [[kAppDelegate getCurrentUIVC] AlertWithTitle:nil message:self.message andOthers:@[@"确定"] animated:YES action:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationOnKick object:nil];
//        KPostNotification(KNotificationOnKick, nil);
    }else if([code isEqualToString:@"122"]){
        //token过期
//        [[kAppDelegate getCurrentUIVC] AlertWithTitle:nil message:self.message andOthers:@[@"确定"] animated:YES action:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationOnKick object:nil];
    }
    
    return  isSuccess;
    
}

#pragma mark ————— 定义返回数据格式，若是加密要用HTTP接受 —————
-(YTKResponseSerializerType)responseSerializerType {
//    if (self.isOpenAES) {
//        return YTKResponseSerializerTypeHTTP;
//    }
    return YTKResponseSerializerTypeJSON;
}
#pragma mark ————— 默认请求方式 POST —————
-(YTKRequestMethod)requestMethod{
    return YTKRequestMethodPOST;
}
//#pragma mark ————— 默认请求体是自身转json —————
//-(id)requestArgument{
//    return [self modelToJSONObject];
//}


-(id)requestArgument{
    return self.params;
}

#pragma mark ————— 请求失败过滤器 —————
-(void)requestFailedFilter{
    //失败处理器
}
#pragma mark ————— 请求成功过滤器 —————
-(void)requestCompleteFilter{
    //解密
//    if (self.isOpenAES) {
//        self.result = aesDecryptWithData(self.responseData);
//    }else{
//        self.result = self.responseJSONObject;
//    }
    /// 统一转换响应对象
    
    self.result =[ResponseModel mj_objectWithKeyValues:self.responseJSONObject];
    [self isSuccess];
//    NSLog(@"请求处理器%@",self.result);
}

#pragma mark ————— 非加密时也要传输的头部内容 也可能不需要，暂时保留 —————
-(NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary{
    //加密header部分
    NSString *contentStr = [[HeaderModel new] modelToJSONString];
    NSString *AESStr = aesEncrypt(contentStr);
    return @{@"header-encrypt-code":AESStr};
    
}

#pragma mark ————— 如果是加密方式传输，自定义request —————
-(NSURLRequest *)buildCustomUrlRequest{
    
    if (!_isOpenAES) {
        return nil;
    }
    NSString *baseUrl = @"";
    if ([self.requestUrl containsString:@"http"]) {
        baseUrl = @"";
    }else{
        baseUrl = URL_main;
    }
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",baseUrl,self.requestUrl]]];
    
//    //加密header部分
//    NSString *headerContentStr = [[HeaderModel new] modelToJSONString];
//    NSString *headerAESStr = aesEncrypt(headerContentStr);
//    [request setValue:headerAESStr forHTTPHeaderField:@"header-encrypt-code"];
    
    NSString *contentStr = [self.requestArgument jsonStringEncoded];
    NSString *random = [NSString randomStringWithLen:16];
    NSString *AESStr = aesEncryptString(contentStr,random);
    AESStr = [AESStr stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
//    AESStr = [AESStr stringByReplacingOccurrencesOfString:@"0X" withString:@"0&#88;"];

    NSString *baseIV = [random base64EncodedString];
//    NSDictionary *dic = @{@"iv":baseIV,@"udata":AESStr};
    NSString *json = [NSString stringWithFormat:@"iv=%@&udata=%@",baseIV,AESStr];
    
    [request setHTTPMethod:@"POST"];
    
//    [request setValue:@"text/encode" forHTTPHeaderField:@"Content-Type"];
    
    NSData *bodyData = [json dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    return request;
    
}

-(NSURLRequest *)currentRequest{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",URL_main,self.requestUrl]]];
    
    //加密header部分
//    NSString *headerContentStr = [[HeaderModel new] modelToJSONString];
//    NSString *headerAESStr = aesEncrypt(headerContentStr);
//    [request setValue:headerAESStr forHTTPHeaderField:@"header-encrypt-code"];
    
    NSString *contentStr = [self.requestArgument jsonStringEncoded];
//    NSString *AESStr = aesEncrypt(contentStr);
    
    [request setHTTPMethod:@"POST"];
    
    [request setValue:@"text/encode" forHTTPHeaderField:@"Content-Type"];
    
    
    NSData *bodyData = [contentStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [request setHTTPBody:bodyData];
    return request;
}

@end
